use axum::Json;
use http::HeaderMap;
use serde_json::Value;
use sea_orm::EntityTrait;
use crate::query;

use super::del_user::{one_str, many_str};

// 单个 = 用户删除自己创建的数据
pub async fn by_id<E:EntityTrait>(header: HeaderMap, entity:E, body:&Value) -> Result<Json<Value>, Json<Value>>{
    one_str(header, entity, "id", query::i32(body, "id")?).await
}
// 删除多个 = 用户删除自己创建的数据
pub async fn by_ids<E:EntityTrait>(header: HeaderMap, entity:E, body:&Value) -> Result<Json<Value>, Json<Value>>{
    let ids = query::vec_i32_json(body, "id")?;
    if ids.len() == 1 {
        let id = ids[0];
        one_str(header, entity, "id", id).await
    }else{
        many_str(header,entity, "id",ids).await
    }
}
// 自动字段
pub async fn by_field_auto<E:EntityTrait>(header: HeaderMap, entity:E, body:&Value, field:&str) -> Result<Json<Value>, Json<Value>>{
    if let Some(f) = body[field].as_i64() {
        one_i32(header,entity, field, f as i32).await
    }else{
        if let Some(f) = query::vec_i32_json_op(&body, field)? {
            many_i32(header, entity, field, f,).await
        }else{
            Err(code::err_("传递参数不正确"))
        }
    }
}
pub async fn by_id_auto<E:EntityTrait>(header: HeaderMap, entity:E, body:&Value) -> Result<Json<Value>, Json<Value>>{ by_field_auto(header, entity, body, "id").await }